﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Core;
using Portal.Systems.Model;
using Portal.Web.Utility;
using Portal.Web.Model;
using Portal.Utility;

namespace Portal.Systems.Render
{
    public class WorkflowPermissionConfigRenderBll : WebPartTemplate
    {
        public override string Id
        {
            get
            {
                return "WorkflowPermissionConfigRenderBll";
            }
        }



        static void CheckPermission(string SiteId)
        {
            try
            {
                string UserId = WebSessionUtility.GetCurrentLoginUser().UserId;
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, SystemPermission.StaticId, SystemPermission.funcManage, SystemPermission.funcManage, UserId, true);
            }
            catch (Exception ex)
            {
                string ReturnData = "<div class=CssAccessDenied>" + ex.Message.ToString() + " <span class=CssBack><a href=\"javascript:history.back(-1);\">Quay lại</a></span></div>\r\n";
                throw new Exception(ReturnData);
            }
        }
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                CheckPermission(SiteId);
                string RoleId = (string)WebEnvironments.Request(SiteId, "RoleId");
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                if (string.IsNullOrEmpty(RoleId)) RoleId = "";
                string ButtonIconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string Html =
                   "<script language=javascript>\r\n" +
                   "    function ClientSave(RoleId)\r\n" +
                   "    {\r\n" +
                   "        SiteId='" + SiteId + "';\r\n" +
                   "        Max = parseInt(document.getElementById('txtMax').value);\r\n"+
                   "        SaveData='';\r\n"+
                   "        for(var iIndex=0;iIndex<Max;iIndex++)\r\n"+
                   "        {\r\n"+
                   "            RoleWorkingWorkflowId = document.getElementById('txtId'+iIndex).value;\r\n" +
                   "            AllowAccess = document.getElementById('chk'+RoleWorkingWorkflowId).checked;\r\n" +
                   "            SaveData=SaveData+RoleWorkingWorkflowId+'~'+AllowAccess+';'\r\n" +
                   "        }\r\n"+
                   "        AjaxOut = Portal.Systems.Render.WorkflowPermissionConfigRenderBll.ServerSideSave(SiteId, RoleId, SaveData).value;\r\n" +
                   "        if(AjaxOut.Error)\r\n" +
                   "        {\r\n" +
                   "            alert(AjaxOut.InfoMessage);\r\n" +
                   "            return;\r\n" +
                   "        }\r\n" +
                   "        alert(AjaxOut.InfoMessage);\r\n" +
                   "    }\r\n" +
                   "</script>\r\n" +
                   "<input type=hidden id=txtRoleId value=\"" + RoleId + "\">\r\n" +
                   "<div class=FormFunction id=divListForm>\r\n" +
                   " <div class=DatabaseFunction>\r\n" +
                   "        <ul>\r\n" +
                   "            <li onclick=\"javascript:ClientSave('"+RoleId+"');\"><img class=image src=\"" + ButtonIconRoot + "/Save.png\"><div>Ghi lại thiết lập</div></li>\r\n" +
                   "            <li onclick=\"javascript:window.open('../../folder.aspx?folder=admin','_self');\"><img class=image src=\"" + ButtonIconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                   "        </ul>\r\n" +
                   "  </div>\r\n" +
                   "  <div id=\"divListWorkflowPermissionConfig\" class=tabcontent>\r\n" +
                         ServerSideReading(SiteId, RoleId).HtmlContent +
                   "  </div>\r\n" +
                   "</div>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = "<div class=CssAccessDenied>" + ex.Message.ToString() + "</div>\r\n";
            }

            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideSave(string SiteId, string RoleId, string SaveData)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string[] Items = SaveData.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                RoleWorkingWorkflowCls[]
                    RoleWorkingWorkflows = new RoleWorkingWorkflowCls[Items.Length];
                for (int iIndex = 0; iIndex < RoleWorkingWorkflows.Length; iIndex++)
                {
                    string[] Ids = Items[iIndex].Split(new char[] { '~' }, StringSplitOptions.RemoveEmptyEntries);
                    RoleWorkingWorkflows[iIndex] = new RoleWorkingWorkflowCls();
                    RoleWorkingWorkflows[iIndex].RoleWorkingWorkflowId = Ids[0];
                    RoleWorkingWorkflows[iIndex].AllowAccess =bool.Parse(Ids[1])?1:0;
                }
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).SaveRoleWorkingWorkflow(SiteId, RoleId, RoleWorkingWorkflows);
                OAjaxOut.InfoMessage = "Cập nhật thành công";
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideReading(string SiteId,string RoleId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                RoleWorkingWorkflowCls[] RoleWorkingWorkflows = SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).ReadingRoleWorkingWorkflow(SiteId,RoleId);
                string Subject = "Thiết lập quyền truy cập vào quy trình";
                string Html =
                   " <input type=hidden id=txtMax value=\"" + RoleWorkingWorkflows.Length + "\">\r\n" +
                   " <h3>" + Subject + "</h3>\r\n" +
                   "            <table>\r\n" +
                   "                <tr>\r\n" +
                   "                    <th class=thIndex>#</th>\r\n" +
                   "                    <th></th>\r\n" +
                   "                    <th>Quy trình</th>\r\n" +
                   "                </tr>\r\n";
                for (int iIndex = 0; iIndex < RoleWorkingWorkflows.Length; iIndex++)
                {
                    Html +=
                       "    <tr>\r\n" +
                       "        <td class=tdIndex>" + (iIndex + 1) + "<input type=hidden id=\"txtId" + iIndex + "\" value=\"" + RoleWorkingWorkflows[iIndex].RoleWorkingWorkflowId + "\"></td>\r\n" +
                       "        <td style=\"width:20px;text-align:center;\"><input type=hidden id=\"txtId" + iIndex + "\" value=\"" + RoleWorkingWorkflows[iIndex].RoleWorkingWorkflowId + "\"><input style=\"cursor:pointer;\" " + (RoleWorkingWorkflows[iIndex].AllowAccess==1?"CHECKED":"") + " type=checkbox id=\"chk" + RoleWorkingWorkflows[iIndex].RoleWorkingWorkflowId + "\"></td>\r\n" +
                       "        <td class=tdCode>" + RoleWorkingWorkflows[iIndex].Subject + "</td>\r\n" +
                       "   </tr>\r\n";
                }
                Html +=
                   "    </table>\r\n";

                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }

            return OAjaxOut;
        }
    }
}
